package com.whut.utils;

import cn.hutool.core.collection.ListUtil;
import com.whut.v_model.ProblemCollectDetail;
import com.whut.v_vo.CalculateLevelPointVO;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

public class pointsCalculate {
    //两个整数相除计算，保留两位小数，返回字符串形式
    public static String calculateFloatToString(int a, int b) {
        float num = (float) a / b;
        DecimalFormat df = new DecimalFormat("0.00");
        String s = df.format(num);
        return s;
    }

    //两个整数相除转化为百分比
    public static String caculateToPercent(int a, int b) {
        double percent = (double) a / (double) b;
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);
        String calPercent = nt.format(percent);
        return calPercent;
    }

    public static String doubleToPercent(double a, int b) {
        double percent = a / (double) b;
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);
        String Percent = nt.format(percent);
        return Percent;
    }

    public static String double2ToPercent(double a, double b) {
        double percent = a / b;
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);
        String Percent = nt.format(percent);
        return Percent;
    }

    public static double getPercentInstance(String weight){
        double percent = 0;
        try {

            if (weight.equals("100%")) {
                percent = 1;
            } else {
                percent = (double) NumberFormat.getPercentInstance().parse(weight);
            }

        } catch (ParseException E) {
            E.printStackTrace();
        }
        return percent;
    }
    public static String getMaxRiskPoint(List<ProblemCollectDetail> list)
    {
        String maxRiskPoint = "";
        int max = 0;
        Map<String,Integer> map = MyMap.RiskPointMap();
        for (ProblemCollectDetail p:list) {
            if(map.get(p.getRiskPoint())>max){
                max = map.get(p.getRiskPoint());
                maxRiskPoint = p.getRiskPoint();
            };
        }
        return maxRiskPoint;
    }

    public static Double getFuzzyResult(List<Double> list1,List<Double> list2)
    {
        System.out.println(list1.size()+"sadasdasadasdasdadasd"+list2.size());
        List<Double> list=new ArrayList<>();
        for(int i=0;i<list1.size();i++)
        {
            if(list1.get(i)<list2.get(i))
            {
                list.add(list1.get(i));
            }else{
                list.add(list2.get(i));
            }
        }
        return Collections.max(list);
    }
    /**
     * @Author chenhuanru
     * @Description //新增四种算子模型
     * @Date 9:28 2020-10-20 09:28:19
     **/
    /**
     * @Author chenhuanru
     * @Description //算子1：先取小再取大
     * @Date 9:32 2020-10-20 09:32:18
     * @Param [list1, list2]
     * @return java.lang.Double
     **/
    public static Double operatorMin(List<Double> list1,List<Double> list2)
    {
        List<Double> list=new ArrayList<>();
        for(int i=0;i<list1.size();i++)
        {
            if(list1.get(i)<list2.get(i))
            {
                list.add(list1.get(i));
            }else{
                list.add(list2.get(i));
            }
        }
        return Collections.max(list);
    }

    /**
     * @Author chenhuanru
     * @Description //算子2：先乘再取大
     * @Date 9:35 2020-10-20 09:35:44
     * @Param [list1, list2]
     * @return java.lang.Double
     **/
    public static Double operatorMutiply(List<Double> list1,List<Double> list2)
    {
        List<Double> list=new ArrayList<>();
        for(int i=0;i<list1.size();i++)
        {
            list.add(list1.get(i)*list2.get(i));
        }
        return Collections.max(list);
    }
    /**
     * @Author chenhuanru
     * @Description //算子3：先取小后求和
     * @Date 9:44 2020-10-20 09:44:56
     * @Param [list1, list2]
     * @return java.lang.Double
     **/
    public static Double operatorSum(List<Double> list1,List<Double> list2)
    {
        //List<Double> list=new ArrayList<>();
        double sum=0;
        for(int i=0;i<list1.size();i++)
        {
            if(list1.get(i)<list2.get(i)){
                //list.add(list1.get(i));
                sum+=list1.get(i);
            }else{
                //list.add(list2.get(i));
                sum+=list2.get(i);
            }
        }
        return sum;
    }

    /**
     * @Author chenhuanru
     * @Description //算子4：先乘再相加（普通矩阵运算）
     * @Date 9:48 2020-10-20 09:48:52
     * @Param [list1, list2]
     * @return java.lang.Double
     **/
    public static Double operatorMutiplyAndSum(List<Double> list1,List<Double> list2)
    {
        double sum=0;
        for(int i=0;i<list1.size();i++)
        {
            sum+=list1.get(i)*list2.get(i);
        }
        return sum;
    }

    public static Double operatorByType(List<Double> list1,List<Double> list2,int type){
        System.out.println("type:"+type);
        double result=0;
        switch (type){
            case 1:{
                result=operatorMin(list1,list2);
                break;
            }
            case 2:{
                result=operatorMutiply(list1,list2);

                break;
            }
            case 3:{
                result=operatorSum(list1,list2);

                break;
            }
            case 4:{
                result=operatorMutiplyAndSum(list1,list2);

                break;
            }
            default:{
                break;
            }
        }
        return result;
    }

    /**
     * @author： wenyihan
     * @description：1-5级通用分数计算公式：Σ（（实得分数/应得分数*权重）*上级应得分数）/Σ权重
    */
    public static String calculatePoint(List<CalculateLevelPointVO> item, String upperPoint){

        if (item.isEmpty()){
            return upperPoint;
        }

        Double weightSum = item.stream()
                                .map(x -> {
                                    String weight = x.getWeight();
                                    weight = weight.substring(0, weight.length()-1);
                                    return Double.parseDouble(weight);
                                })
                                .collect(Collectors.summingDouble(x->x.doubleValue()));
        //Σ（（实得分数/应得分数*权重）*上级应得分数）
        Double numerator = item.stream()
                                .map(x -> {
                                    //应得分
                                    Double point = Double.parseDouble(x.getPoint());
                                    //实得分
                                    Double pointCa = Double.parseDouble(x.getPointCa());
                                    String weight = x.getWeight();
                                    weight = weight.substring(0, weight.length()-1);
                                    Double weightD = Double.parseDouble(weight);
                                    if (point == 0d){
                                        return 0;
                                    }
                                    return pointCa / point * weightD * Double.parseDouble(upperPoint);
                                })
                                .collect(Collectors.summingDouble(x-> x.doubleValue()));
        Double point = numerator/weightSum;
        return String.valueOf(point);
    }

    /**
     * @author： wenyihan
     * @description：根据problemNum、problemQua、deductPoint计算扣除分数
     * @date： 21:38 2021/2/2
     * @param： [problemNum, problemQua, deductPoint]
     * @return： java.lang.String
     */
    public static String getReducePoint(String problemNum, String problemQua, String deductPoint){
        //todo 这里这样写是因为，pcd表里有problemNum、problemQua、deductPoint为空的检查项，在这里先置为0，出报告
        if (StringUtils.isBlank(problemQua) || StringUtils.isBlank(problemNum) || StringUtils.isBlank(deductPoint)){
            return "0";
        }
        String[] newProblemNum = problemNum.split(";");
        String[] newDeductPoint = deductPoint.split(";");
        int a=0;
        for(int i=0;i<newProblemNum.length;i++){
            if(StringUtils.contains(newProblemNum[i],problemQua)){
                a=i;
                break;
            }
        }
        return newDeductPoint[a];

        //String[] problemNumArray = problemNum.replaceAll("\\r", "").replaceAll(" ", "").split(";");
        //String[] deductPointArray = deductPoint.replaceAll("\\r", "").replaceAll(" ", "").split(";");
        //List<String> problemNumList = Arrays.asList(problemNumArray);
        //int[] indexArray = ListUtil.indexOfAll(problemNumList, problemQua::equals);
        //String reducePoint = deductPointArray[indexArray[0]];
        //return reducePoint;
    }

}
